<template>
    <a-form>
        <a-form-item label="请输入旧密码">
            <a-input type="password" v-model="form.oldPaasword"></a-input>
        </a-form-item>

        <a-form-item label="请输入新密码">
            <a-input type="password" v-model="form.newPassword"></a-input>
        </a-form-item>

        <a-form-item label="请确认新密码">
            <a-input type="password" v-model="form.confirmPassword"></a-input>
        </a-form-item>
    </a-form>
</template>

<script>
    import {Form,Input,message} from 'ant-design-vue';

    import persistentUtil from '@/common/persistentUtil.js'

    export default {
        name: "UpdatePasswordComponent",
        components:{
          AForm:Form,
          AFormItem:Form.Item,
          AInput:Input
        },

        data() {
            return {
                form: {
                    oldPaasword: '',
                    newPassword: '',
                    confirmPassword: ''
                }
            };
        },


        methods: {
            verify() {

                if (this.form.oldPaasword === '') {
                    message.error("请输入旧密码");
                    return false;
                }
                if (this.form.newPassword === '') {
                    message.error("请输入新密码");
                    return false;
                }
                if (this.form.confirmPassword != this.form.newPassword) {
                    message.error("两次新密码输入不一致");
                    return false;
                }

                return true;
            }
            ,
            submit() {

                if (this.verify()) {


                    let params = new URLSearchParams();
                    params.append("oldPassword",this.form.oldPaasword);
                    params.append("newPassword",this.form.newPassword);

                    this.$axios.post("/auth/updatePassword",params)
                        .then(d => {

                            let result = d.data;

                            if (result.status == 0) {

                                message.success(result.message);
                                let token = result.data;
                                persistentUtil.saveToken(token);

                                this.$emit("ok");

                            } else {

                                message.error(result.message);
                            }

                        })
                        .catch(e => {

                            message.error("更新密码失败");
                        });

                }
            }
        }
    }
</script>

<style scoped>

</style>